/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package codegener.comp;

import codegener.CodeGener;
import codegener.entity.GenerBean;
import codegener.util.JsonUtils;
import java.io.File;
import java.util.Map;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author 32181
 */
public class GenerForm extends javax.swing.JPanel {

    private GenerBean generBean=new GenerBean();
    private DirJDialog dirDialog=new DirJDialog(CodeGener.getFrame(),true,this);
    
    /**
     * Creates new form generForm
     */
    public GenerForm() {
        initComponents();
        
        File conf=new File("./conf/conf.json");
        
        if(conf.exists()){
            try{
                String content=FileUtils.readFileToString(conf, "UTF-8");
                Map<String,Object> set=JsonUtils.parseJsonToMap(content);
                generBean.setType(String.valueOf(set.get("type")));
                generBean.setDir(String.valueOf(set.get("dir")));
                generBean.setBeanSuffix(String.valueOf(set.get("beanSuffix")));
                generBean.setMapperSuffix(String.valueOf(set.get("mapperSuffix")));
                generBean.setServiceSuffix(String.valueOf(set.get("serviceSuffix")));
                generBean.setControllerSuffix(String.valueOf(set.get("controllerSuffix")));
                generBean.setTabQian(String.valueOf(set.get("tabQian")));
                generBean.setTabHou(String.valueOf(set.get("tabHou")));

                typeCombo.setSelectedItem(set.get("type"));
                dirTxt.setText(generBean.getDir());
                controllerTxt.setText(generBean.getControllerSuffix());
                mapperTxt.setText(generBean.getMapperSuffix());
                serviceTxt.setText(generBean.getServiceSuffix());
                beanTxt.setText(generBean.getBeanSuffix());
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    public void setDir(String dir){
        dirTxt.setText(dir);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        typeCombo = new javax.swing.JComboBox<>();
        jLabel2 = new javax.swing.JLabel();
        dirBtn = new javax.swing.JButton();
        okBtn = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        controllerTxt = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        serviceTxt = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        mapperTxt = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        beanTxt = new javax.swing.JTextField();
        dirTxt = new javax.swing.JTextField();
        表名转实体名 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        qianTxt = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        houTxt = new javax.swing.JTextArea();

        jLabel1.setText("字段转换方式：");

        typeCombo.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "驼峰式", "全大写", "全小写", "不转换" }));

        jLabel2.setText("生成文件地址：");

        dirBtn.setText("选择文件目录");
        dirBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dirBtnActionPerformed(evt);
            }
        });

        okBtn.setText("保  存");
        okBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okBtnActionPerformed(evt);
            }
        });

        jLabel3.setText("Controller包后缀：");

        controllerTxt.setText("controller");

        jLabel4.setText("Service包后缀：");

        serviceTxt.setText("service");

        jLabel5.setText("Mapper包后缀：");

        mapperTxt.setText("mapper");

        jLabel6.setText("Bean包后缀：");

        beanTxt.setText("bean");

        表名转实体名.setText("表名转实体名去前缀：");

        jLabel7.setText("表名转实体名去后缀：");

        qianTxt.setColumns(20);
        qianTxt.setRows(5);
        qianTxt.setToolTipText("多个以英文逗号分隔");
        jScrollPane1.setViewportView(qianTxt);

        houTxt.setColumns(20);
        houTxt.setRows(5);
        houTxt.setToolTipText("多个以英文逗号分隔");
        jScrollPane2.setViewportView(houTxt);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(表名转实体名)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel2)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(typeCombo, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(dirTxt)
                            .addComponent(serviceTxt, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(controllerTxt)
                            .addComponent(beanTxt, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(mapperTxt)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(okBtn, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(dirBtn, javax.swing.GroupLayout.Alignment.TRAILING)))
                            .addComponent(jScrollPane2))))
                .addGap(96, 96, 96))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(typeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(dirTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2)
                .addComponent(dirBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(controllerTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(serviceTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(mapperTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(beanTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(表名转实体名)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(okBtn)
                        .addGap(31, 31, 31))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(84, Short.MAX_VALUE))))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed
        // TODO add your handling code here:
        generBean.setType(String.valueOf(typeCombo.getSelectedItem()));
        generBean.setDir(dirTxt.getText());
        generBean.setControllerSuffix(controllerTxt.getText());
        generBean.setMapperSuffix(mapperTxt.getText());
        generBean.setServiceSuffix(serviceTxt.getText());
        generBean.setBeanSuffix(beanTxt.getText());
        generBean.setTabQian(String.valueOf(qianTxt.getText()));
        generBean.setTabHou(String.valueOf(houTxt.getText()));
        
         File confDir=new File("./conf");
        if(!confDir.exists()){
            confDir.mkdir();
        }
        try{
            FileUtils.writeStringToFile(new File("./conf/conf.json"),JsonUtils.toJsonStr(generBean), "UTF-8");
        }catch(Exception e){
            e.printStackTrace();
        }
    }//GEN-LAST:event_okBtnActionPerformed

    public GenerBean getGenerBean(){
        return generBean;
    }
    
    private void dirBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dirBtnActionPerformed
        // TODO add your handling code here:
        dirDialog.setVisible(true);
    }//GEN-LAST:event_dirBtnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField beanTxt;
    private javax.swing.JTextField controllerTxt;
    private javax.swing.JButton dirBtn;
    private javax.swing.JTextField dirTxt;
    private javax.swing.JTextArea houTxt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField mapperTxt;
    private javax.swing.JButton okBtn;
    private javax.swing.JTextArea qianTxt;
    private javax.swing.JTextField serviceTxt;
    private javax.swing.JComboBox<String> typeCombo;
    private javax.swing.JLabel 表名转实体名;
    // End of variables declaration//GEN-END:variables
}
